<html>
<head>
<title>Example: How to sort trees and other controls</title>
<link rel="stylesheet" type="text/css" href="../default.css"/>
<script type="text/javascript" src="../../src/main/js/loader.js"></script>
<script type="text/javascript">

	load('core.ajax.Request');
	load('core.ui.Tree');
	load('core.ui.TreeItem');

	function init() {
		tree = new core.ui.Tree();
		tree.show();
		sortTree('default');
	}
	
	function sortTree(how) {
		tree.removeItems();
		tree.setText('Loading...');
		var request = new core.ajax.Request('treedata.' + how + '.json');
		request.addRequestListener({
			onRequestComplete : function(request, response) {
				if ('OK' == response.status) {
					tree.setText('');
					if (response.items) {
						for (var i = 0; i < response.items.length; i++) {
							addItem(response.items[i], tree);
						}
					}
				} else {
					tree.setText('Failed to load tree data from server (%s)', response.message);
				}
			}
		});
		request.send();
	}
	
	function addItem(data, tree) {
		
		var item = new core.ui.TreeItem(tree);
		item.setText(data.text);
		tree.addItem(item);
		
		if (data.items) {
			for (var i = 0; i < data.items.length; i++) {
				addItem(data.items[i], item);
			}
		}
		
		item.setState(true);
	}
	
</script>
</head>

<body onload="init()">

	Sort tree:
	<a href="javascript:sortTree('asc')">Ascending</a> | 
	<a href="javascript:sortTree('desc')">Descending</a> | 
	<a href="javascript:sortTree('default')">Default</a>
	<br><br>
	
</body>
</html>
